package com.jl.card.provider.jl2g;

import com.jl.card.file.IFileInfo;
import com.jl.util.HexUtil;

public class JL2GFileInfo implements IFileInfo {

	private byte[] fci = null;

	private static String[] access = new String[] { "ALW", "CHV1", "CHV2",
			"RFU", "ADM", "ADM", "ADM", "ADM", "ADM", "ADM", "ADM", "ADM",
			"ADM", "ADM", "ADM", "NEV" };

	private static String[] fileType = new String[] { "RFU", "MF", "DF", "",
			"EF" };
	private static String[] fileStruct = new String[] { "Transparent",
			"Linear fixed", "", "Cyclic" };
	private int filelength = -1;
	private short fileid = -1;

	private int filetype = 8;
	private int filestruct = 0;
	private int filestatue = 0;
	private int recordsize = 0;

	private String acREAD = "";
	private String acUPDATE = "";
	private String acINCREASE = "";
	private String acINVALIDATE = "";
	private String acREHABILITATE = "";

	/**
	 * @param fci
	 */
	public JL2GFileInfo(byte[] fci) {
		this.fci = fci.clone();
		if (fci[0] == 0x00 && fci[1] == 0x00) {
			filelength = HexUtil.makeInt(fci, 2, 2);
			fileid = HexUtil.makeShort(fci, 4);
			filetype = fci[6];
			if (fci.length == 0x0F) {// EF
				acREAD = access[(fci[8] >> 4) & 0x0F];
				acUPDATE = access[(fci[8]) & 0x0F];
				acINCREASE = access[(fci[9] >> 4) & 0x0F];
				acINVALIDATE = access[(fci[10]) & 0x0F];
				acREHABILITATE = access[(fci[10] >> 4) & 0x0F];

				filestatue = fci[11];
				filestruct = fci[13];
				recordsize = fci[14] & 0xFF;
			} else {// DF

			}
		}
	}

	@Override
	public short getFileID() {
		return fileid;
	}

	@Override
	public byte[] getHeader() {
		return fci;
	}

	@Override
	public int getLength() {
		return filelength;
	}

	@Override
	public int getRecordSize() {
		return recordsize;
	}

	@Override
	public boolean isCyclic() {
		return (filestruct & 3) == 3;
	}

	@Override
	public boolean isDirectory() {
		return filetype == 0x01 || filetype == 0x02;
	}

	@Override
	public boolean isTransparent() {
		return filestruct == 0x00;
	}

	@Override
	public boolean isVariable() {
		return false;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("File ID      : ").append(HexUtil.toHex(fileid, 2))
				.append('\n');
		if (isDirectory()) {
			sb.append("Unused Size  : ").append(filelength + "").append('\n');
		} else {
			sb.append("File Size    : ").append(filelength + "").append('\n');
		}
		sb.append("File Type    : ").append(fileType[filetype]);
		if (!isDirectory()) {
			sb.append('(').append(fileStruct[filestruct]).append(')')
					.append('\n');
		}

		if (!isTransparent()) {
			sb.append("Record Size  : ").append(recordsize + "").append('\n');
			sb.append("Record Num   : ").append(filelength / recordsize + "")
					.append('\n');
		}

		if (!isDirectory()) {
			sb.append("READ         : ").append(acREAD).append('\n');
			sb.append("UPDATE       : ").append(acUPDATE).append('\n');
			sb.append("INCREASE     : ").append(acINCREASE).append('\n');
			sb.append("INVALIDATE   : ").append(acINVALIDATE).append('\n');
			sb.append("REHABILITATE : ").append(acREHABILITATE).append('\n');
		}

		return sb.toString().trim();
	}

}
